<?php

namespace App\Controller\ClickMonitor;

use App\Constants\ChannelTypeEnum;
use App\Constants\ErrorCode;
use App\Controller\AbstractController;
use App\Model\Business\AppAdAccount;
use App\Model\Business\Application;
use App\Model\Business\OppoAd;
use App\Model\Report\OppoDevice;
use App\Services\Impl\CommonService;
use App\Utils\OppoUtil;
use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;

class OppoController extends AbstractController
{
    #[Inject]
    protected OppoUtil $oppoUtil;

    #[Inject]
    protected CommonService $commonService;

    public function click(): ResponseInterface
    {
        $appId     = $this->request->input("appId", "");
        $channel   = $this->request->input("channel", "");
        $imei      = $this->request->input("imei", "");
        $oaid      = $this->request->input("oaid", "");
        $adId      = $this->request->input("adId", "");
        $adName    = $this->request->input("adName", "");
        $requestId = $this->request->input("requestId", "");
        $ua        = $this->request->input("ua", "");
        $ip        = $this->request->input("ip", "");

        var_dump($this->request->all());

        throw_error_if(!$appId || !$channel, "未传应用信息");

        if ($oaid == "00000000-0000-0000-0000-000000000000") {
            $oaid = "";
        }

        // 如果oaid没有并且ua或ip没有
        throw_error_if(!$oaid && (!$ua || !$ip), '请求错误');

        $app = Application::findOrFail($appId);
        $projectId = $app->project_id;

        $adPlanId = "";
        if ($adId) {
            // 先获取库中是否有这个广告创意数据
            $oppoAd = OppoAd::query()->where("project_id", $projectId)
                ->where("app_id", $appId)
                ->where("ad_id", $adId)
                ->first();

            // 如果有，直接取计划ID
            if ($oppoAd) {
                $adPlanId = $oppoAd->ad_plan_id;
            } else {
                // 获取当前应用oppo渠道所有广告账户
                $adAccount = AppAdAccount::query()
                    ->where("project_id", $projectId)
                    ->where("app_id", $appId)
                    ->where("channel_type", ChannelTypeEnum::OPPO->value)
                    ->whereRaw("FIND_IN_SET('$channel', channels)")
                    ->first();

                // 查询该广告详情
                $adDetail = $this->oppoUtil->getAdDetail($adAccount->params['owner_id'], $adAccount->params['api_id'],
                    $adAccount->params['api_key'], $adId);

                $adPlanId = $adDetail['adPlanId'] ?? "";

                OppoAd::create([
                    'project_id' => $projectId,
                    'app_id'     => $appId,
                    'ad_id'      => $adId,
                    'ad_name'    => $adName,
                    'ad_plan_id' => $adPlanId,
                ]);
            }
        }

        OppoDevice::create([
            'project_id' => $projectId,
            'app_id'     => $appId,
            'channel'    => $channel,
            'imei'       => $imei,
            'oaid'       => $oaid,
            'ad_id'      => $adId,
            'ad_name'    => $adName,
            'ad_plan_id' => $adPlanId,
            'request_id' => $requestId,
            'ua'         => $ua,
            'ip'         => $ip,
        ]);

        $this->commonService->insertBigDevice($projectId, $appId, $oaid, $ua, $ip,
            ChannelTypeEnum::OPPO->value, $channel, $adId, $adName, $adPlanId, '');

        return $this->succeed();
    }
}